<!doctype html>
<head>
	<title>Tube Example</title>
	<script src="../dist/threebox.js" type="text/javascript"></script>
	<script src="config.js"></script>

	<script src='https://api.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.js'></script>
	<link href='https://api.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.css' rel='stylesheet' />
	<style>
		body, html { 
			width: 100%;
			height: 100%;
			margin: 0;
		}
		#map { 
			width: 100%;
			height: 100%;
		}
	</style>
</head>
<body>
	<div id='map' class='map'></div>

	<script>

		if(!config) console.error("Config not set! Make a copy of 'config_template.js', add in your access token, and save the file as 'config.js'.");
		
		mapboxgl.accessToken = config.accessToken;
		var origin = [-95.97299, 36.15031,0];

		var map = new mapboxgl.Map({
		  container: 'map',
		  style: 'mapbox://styles/mapbox/light-v9',
		  center: origin,
		  zoom: 14,
		  pitch: 60,
		  heading: 41
		});

		//generate a spiral line geometry (not essential for understanding this demo)

		var lineGeometry = [];

		for (var l = 0; l<200; l++) {

			var delta = [
				Math.sin(l/5) * l/100000, 
				Math.cos(l/5) * l/100000, 
				l*5
			]

			var newCoordinate = origin.map(function(d,i){
				return d + delta[i]
			})
			lineGeometry.push(newCoordinate)
		}

		console.log("Tube's line geometry: ", lineGeometry);



		map.on('style.load', function() {

			map.addLayer({
				id: 'custom_layer',
				type: 'custom',
				renderingMode: '3d',
				onAdd: function(map, mbxContext){

					window.tb = new Threebox(
						map, 
						mbxContext,
						{defaultLights: true}
					);

					var tubeOptions = {
						geometry: lineGeometry,
						radius: 1,
						sides: 8,
						material: 'MeshPhysicalMaterial',
						color:'#00ffff'
					}

					tube = tb.tube(tubeOptions);
					// tube.material.wireframe = true
					tb.add(tube);

				},
				
				render: function(gl, matrix){
					tb.update();
				}
			});
		});

	</script>
</body>